﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EIS.Data;
using EIS.DatabaseHelper;
using EIS.Obj;

namespace EIS.ViewControl.Grid
{
    public class ViewGridController
    {
        public string FunctionID;
        public DataSet TransactionDataSet;
        public ViewGridDisplay view;
        public ViewGridController(string functionID, ViewGridDisplay viewIn)
        {
            FunctionID = functionID;
            view = viewIn;
        }

        //Must Be called after set the two Time Pickers in view
        public void GetLocalData()
        {
            DateTime dateFrom = view.GetDatePickerFrom();

            //dateFrom = new DateTime(dateFrom.Year,dateFrom.Month,dateFrom.Day, DateTime.Now.Hour,DateTime.Now.Minute,DateTime.Now.Second);

            long tickFrom = dateFrom.ToUniversalTime().Ticks;

            DateTime dateTo = view.GetDatePickerTo();

            dateTo = new DateTime(dateTo.Year, dateTo.Month, dateTo.Day, 23, 59, 59);

            long tickTo = dateTo.ToUniversalTime().Ticks;
            var dbhandler = new DBHandler();
            TransactionDataSet = dbhandler.SelectSimpleBetweenTimStmp(FunctionID, tickFrom, tickTo);
            view.dataDs = TransactionDataSet;
            view.dataDs.Tables[0].DefaultView.Sort = EISConstant.EISJavaServerMainID + " DESC";
        }

        public string GetDefaultLayout()
        {
            var dbHandler = new DBHandler();

            DataSet ds = dbHandler.SelectSimple(EISConstant.ClientLayoutTableName);
            var dsToShow = ds.Clone();

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (dr["FunctionID"].Equals(FunctionID) && dr["Default"].ToString() == "Y")
                {
                    return dr["Layout"].ToString();
                }
            }
            return string.Empty;
        }

        public void InitSetDatePickers()
        {
            
            view.SetDatePickerTo(new DateTime(DateTime.Now.Date.Year,DateTime.Now.Date.Month,DateTime.Now.Date.Day,23,59,59));
            view.SetDatePickerFrom(DateTime.Now.Date.AddDays(0 - ClientUtility.ClientConfig.Instance.DisplayDaysForFunctionDate) );
        }


        

        private void BgwSaveDataMarkReadDoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = MarkRecordReadUnread.MarkReadUnread(FunctionID, (DataTable)e.Argument);
        }
        private void BgwSaveDataMarkAllReadDoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = MarkRecordReadUnread.MarkAllAsRead(FunctionID);
        }

        public void MarkReadUnread(DataTable dt)
        {
            BackgroundWorker bgwSaveDataMarkRead = new BackgroundWorker();
            bgwSaveDataMarkRead.DoWork += BgwSaveDataMarkReadDoWork;
            bgwSaveDataMarkRead.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgwSaveDataMarkRead_RunWorkerCompleted);

            bgwSaveDataMarkRead.RunWorkerAsync(dt);
            //return MarkRecordReadUnread.MarkReadUnread(FunctionID, dt);


            
        }
        public void MarkAllAsRead()
        {
            BackgroundWorker bgwSaveDataMarkRead = new BackgroundWorker();
            bgwSaveDataMarkRead.DoWork += BgwSaveDataMarkAllReadDoWork;
            bgwSaveDataMarkRead.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgwSaveDataMarkAllRead_RunWorkerCompleted);
            bgwSaveDataMarkRead.RunWorkerAsync(null);
        }

        private void bgwSaveDataMarkAllRead_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // Refresh Current View
            view.ResetLayout();
        }

        private void bgwSaveDataMarkRead_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //MessageBox.Show("Finished");
        }
    }
}
